package com.lcxw.hxzb.friendcircle.project.system.FeignService;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzb.friendcircle.project.system.FeignService.ImManager;
import com.lcxw.hxzb.friendcircle.project.system.FeignService.TencentImConfigBean;
import com.tencentyun.TLSSigAPIv2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Random;

@Component
@Slf4j
public class TencentImRealization {

    @Resource
    ImManager imManager;



    public void sendmsg(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(TencentImConfigBean.APPID,
            TencentImConfigBean.KEY);
        long random = new Random().nextLong();
        String admin = TencentImConfigBean.ADMIN;
        String key = tlsSigAPIv2.genSig(admin, TencentImConfigBean.EXPIRE);
        String result = imManager
            .sendmsg(TencentImConfigBean.APPID, admin, key, random, imBody);
        log.info("发送消息结果：" + result);
    }

    /**
     * 4、全员推送消息
     */
    public void sendAllMember(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(com.lcxw.hxzbcommon.entity.TencentImConfigBean.APPID,
                com.lcxw.hxzbcommon.entity.TencentImConfigBean.KEY);
        long random = new Random().nextLong();
        String admin = com.lcxw.hxzbcommon.entity.TencentImConfigBean.ADMIN;
        String key = tlsSigAPIv2.genSig(admin, com.lcxw.hxzbcommon.entity.TencentImConfigBean.EXPIRE);
        String result = imManager
                .sendAllMember(com.lcxw.hxzbcommon.entity.TencentImConfigBean.APPID, admin, key, random, imBody);
        log.info("全员推送消息：" + result);
    }

    /**
     * 构造imBody消息的方法
     */
    public static JSONObject  generateImBody(String msg, String fromUserId,String toUserId) {
        JSONObject imBody = new JSONObject();
        JSONArray msgBody = new JSONArray();
        imBody.put("From_Account", fromUserId);
        imBody.put("To_Account", toUserId);
        imBody.put("SyncOtherMachine",2);
        imBody.put("MsgRandom", new Random().nextInt(10000000));
        JSONObject msgCustomElem = new JSONObject();
        msgCustomElem.put("MsgType", "TIMCustomElem");

        JSONObject msgContentObj = new JSONObject();
        msgContentObj.put("Data", "message");
        msgContentObj.put("Ext", msg);
        msgCustomElem.put("MsgContent", msgContentObj);

        msgBody.add(msgCustomElem);
        imBody.put("MsgBody", msgBody);

        return imBody;

    }
    /**
     * 全员推送消息体
     */
    public JSONObject generateIm(String msg,String data) {
        JSONObject imBody = new JSONObject();
        JSONArray msgBody = new JSONArray();
        JSONObject msgCustomElem = new JSONObject();
        msgCustomElem.put("MsgType", "TIMCustomElem");
        JSONObject msgContentObj = new JSONObject();
        msgContentObj.put("Data", data);
        msgContentObj.put("Ext", msg);
        msgCustomElem.put("MsgContent", msgContentObj);
        msgBody.add(msgCustomElem);
        imBody.put("MsgBody", msgBody);
        return imBody;

    }
    /**
     * 构造系统消息的方法
     */
    public  JSONObject generateUserBody(String msg, String userId) {
        JSONObject imBody = new JSONObject();
        JSONArray msgBody = new JSONArray();
        imBody.put("To_Account", userId);
        imBody.put("SyncOtherMachine",2);
        imBody.put("MsgRandom", new Random().nextInt(10000000));
        JSONObject msgCustomElem = new JSONObject();
        msgCustomElem.put("MsgType", "TIMCustomElem");
        JSONObject msgContentObj = new JSONObject();
        msgContentObj.put("Data", "message");
        msgContentObj.put("Ext", msg);
        msgCustomElem.put("MsgContent", msgContentObj);
        msgBody.add(msgCustomElem);
        imBody.put("MsgBody", msgBody);
        return imBody;
    }
}
